﻿using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Weapon : MonoBehaviour
{
    int Attack;
    public bool canAttack;
    BoxCollider2D colli;
    CameraController2D m_camera;
    private Animator animator;
    private float botttomFrameTime;
    private void Awake()
    {
        m_camera = Camera.main.GetComponent<CameraController2D>();
        colli = GetComponent<BoxCollider2D>();
        animator = GetComponent<Animator>();
        colli.enabled = false;
    }
    private void Update()
    {
        if (botttomFrameTime>0)
        {
            botttomFrameTime -= Time.deltaTime;
            if (botttomFrameTime<=0)
            {
                botttomFrameTime = 0;
                animator.speed = 1;
            }
        }
    }
    public void SetAttack(int atk)
    {
        Attack = atk;
    }
    public void SetWeaponActive(bool isActive)
    {
        colli.enabled = isActive;
        if (isActive)
            canAttack = true;
        else
            canAttack = false;
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag=="Enemy")
        {
            if (canAttack)
            {
                IDamage damage = collision.transform.GetComponent<IDamage>();
                damage.OnDamage(transform.parent.gameObject, transform.position, Attack);
                m_camera.CameraShake();
                SetBottomFrameTime(0.1f);
                canAttack = false;
            }
           
        }
    }


    /// <summary>
    /// 顿帧
    /// </summary>
    /// <param name="time"></param>
    public void SetBottomFrameTime(float time)
    {
        animator.speed = 0;
        botttomFrameTime = time;
    }
}
